//
// Created by francklinson on 2021/10/21 AT 11:26.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>
#include <string>

using namespace std;

class Solution {
public:
    int minimumDifference(vector<int> &nums, int k) {
        if (nums.size() == 0)return 0;
        if (k == 0) return 0;
        sort(nums.begin(), nums.end());
        int ans = INT_MAX;
        for (int i = 0; i <= nums.size() - k; ++i) {
            ans = min(ans, nums[i + k - 1] - nums[i]);
        }
        return ans;
    }
};

int main() {
    vector<int> nums{9, 4, 1, 7};
    Solution solution;
    cout << solution.minimumDifference(nums, 2) << endl;
    return 0;
}

